package com.stoken.web.control;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.stoken.domain.MemberBaseInfo;
import com.stoken.domain.MemberLeaveMessage;
import com.stoken.domain.MemberLeaveReply;
import com.stoken.domain.MemberLeaveType;
import com.stoken.domain.TravelLeaveMessage;
import com.stoken.domain.TravelLeaveReply;
import com.stoken.domain.TravelLeaveType;
import com.stoken.service.MemberBaseInfoService;
import com.stoken.service.TravelLeaveMessageService;
import com.stoken.service.TravelLeaveReplyService;
import com.stoken.service.TravelLeaveTypeService;
import com.stoken.web.StokenUtils;
import com.symbio.commons.Compositor;
import com.symbio.commons.Page;
import com.symbio.commons.Compositor.CompositorType;
import com.symbio.utils.WebUtils;

@Controller
@RequestMapping("/tlMR")
public class TravelLeaveMRControl {
	
	@Autowired
	private MemberBaseInfoService memberBaseInfoService;
	
	@Autowired
	private TravelLeaveMessageService travelLeaveMessageService;
	
	@Autowired
	private TravelLeaveReplyService travelLeaveReplyService;
	
	@Autowired
	private TravelLeaveTypeService travelLeaveTypeService;
	/**
	 * 留言主页
	 */
	@RequestMapping("/Index")
	public String index(HttpServletRequest request){

		return "operat/travelMessage";
	}
	
	/**
	 * 留言添加主页
	 */
	@RequestMapping("/addIndex")
	public String addIndex(HttpServletRequest request){
		List<TravelLeaveType> leaveType = travelLeaveTypeService.findAll();
		request.setAttribute("leaveType", leaveType);
		return "operat/travelWriteMessage";
	}
	
	/**
	 * 添加留言
	 */
	@RequestMapping("/add")
	public String addTLM(HttpServletRequest request,TravelLeaveMessage tlm,String lType){
		//获得session里面的user信息
		MemberBaseInfo mbi = (MemberBaseInfo) request.getSession().getAttribute(StokenUtils.LOGINACCOUNT);
		tlm.setBaseUsername(mbi);
	    tlm.setIsReply(0);
	    tlm.setLeaveTime(new Date());
	    TravelLeaveType type = new TravelLeaveType();
	    type.setId(new Long(lType));
		tlm.setLeaveType(type );
		travelLeaveMessageService.save(tlm);
	    
	    
		return "operat/travelMessage";
	}
	
	/**
	 * 查询留言
	 * @param request
	 * @param modeMape
	 * @return
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request,ModelMap modeMape)
	{
		return "operat/leaveMessage";
	}
	
	/**
	 * 分页查询留言
	 * @param request
	 * @param modeMape
	 * @return
	 */
	@RequestMapping("/listPage")
	public String listPage(HttpServletRequest request,ModelMap modeMape)
	{
		MemberBaseInfo mbi = (MemberBaseInfo) request.getSession().getAttribute(StokenUtils.LOGINACCOUNT);
		Page<TravelLeaveMessage> pageData = new Page<TravelLeaveMessage>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("leaveTime",CompositorType.DESC);
		pageData.setCompositor(compositor);
		
		pageData = travelLeaveMessageService.find(pageData,mbi.getId());		
	    modeMape.addAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		modeMape.addAttribute("pagination", page.showPages());
	    
		return "operat/travelMessagePage";
	}
	
	/**
	 * 查看留言
	 * @param request
	 * @param mlmId
	 * @return
	 */
	@RequestMapping("/loadMLMessage")
	public String loadMLMessage(HttpServletRequest request,String mlmId)
	{
		TravelLeaveMessage mlm = travelLeaveMessageService.get(new Long(mlmId));
		request.setAttribute("mlm", mlm);
//		TravelLeaveReply mlr = travelLeaveMessageService.getMessageReply(mlmId);
//		request.setAttribute("mlr", mlr);
		return "";
	}
	
	/**
	 * 添加回复
	 * @param request
	 * @param mlr
	 * @param mlmId
	 * @return
	 */
	@RequestMapping("/addReply")
	public String addReply(HttpServletRequest request,TravelLeaveReply mlr,String mlmId){
		TravelLeaveMessage mlm = travelLeaveMessageService.get(new Long(mlmId));
		mlm.setIsReply(1);
		//mlr.setLeaveTopic(mlm);
		travelLeaveReplyService.save(mlr);
		return "";
	}
	
	@RequestMapping("/delMessage")
	public String delMessage(HttpServletRequest request,String id,ModelMap modeMape){
		travelLeaveMessageService.delete(new Long(id));
		MemberBaseInfo mbi = (MemberBaseInfo) request.getSession().getAttribute(StokenUtils.LOGINACCOUNT);
		Page<TravelLeaveMessage> pageData = new Page<TravelLeaveMessage>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("leaveTime",CompositorType.DESC);
		pageData.setCompositor(compositor);
		
		pageData = travelLeaveMessageService.find(pageData,mbi.getId());		
	    modeMape.addAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		modeMape.addAttribute("pagination", page.showPages());
	    
		return "operat/travelMessagePage";

	}
	
	@RequestMapping("/merchant/traveIndex")
	public String traveMerchantIndex(HttpServletRequest request){
	   return "merchant/leaveMessage/leaveMessage";
	}
	
	/**
	 * 分页查询留言
	 * @param request
	 * @param modeMape
	 * @return
	 */
	@RequestMapping("/merchant/listPage")
	public String listMerchantPage(HttpServletRequest request,ModelMap modeMape)
	{
		Page<TravelLeaveMessage> pageData = new Page<TravelLeaveMessage>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("leaveTime",CompositorType.DESC);
		pageData.setCompositor(compositor);
		
		pageData = travelLeaveMessageService.find(pageData);		
	    modeMape.addAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		modeMape.addAttribute("pagination", page.showPages());
	    
		return "merchant/leaveMessage/leaveMessagePage";
	}
}
